<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
		:type="!showDetail ? 'list' : 'hide'"
	>
		<template slot="query">
			<el-form ref="form" :inline="true" :model="queryData">
				<el-form-item
					:label="$t('common.belong_merchant') + ':'"
					prop="merchantId"
				>
					<merchant-select
						v-model="queryData.merchantId"
					></merchant-select>
				</el-form-item>
				<el-form-item>
					<el-select
						v-model="queryData.timeType"
						size="medium"
						style="width:130px"
					>
						<el-option
							:label="$t('agent.apply_time')"
							:value="0"
						></el-option>
						<el-option
							:label="$t('agent.audit_complete_time')"
							:value="1"
						></el-option>
					</el-select>
					<el-date-picker
						v-model="searchTime"
						size="medium"
						:picker-options="pickerShortcut2"
						format="yyyy-MM-dd HH:mm:ss"
						type="datetimerange"
						range-separator="-"
						:start-placeholder="$t('common.start_date')"
						:end-placeholder="$t('common.end_date')"
						align="right"
						:default-time="defaultTime"
					></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-input
						v-model="queryData.operatorName"
						clearable
						size="medium"
						:placeholder="$t('common.please_enter')"
					>
						<el-select
							slot="prepend"
							v-model="queryData.operatorType"
							style="width: 120px"
							:placeholder="$t('common.please_choose')"
						>
							<el-option
								:label="$t('agent.apply_people')"
								:value="0"
							></el-option>
							<el-option
								:label="$t('agent.audit_people')"
								:value="1"
							></el-option>
							<el-option
								:label="$t('agent.agent_account')"
								:value="2"
							></el-option>
							<el-option
								:label="$t('agent.audit_number')"
								:value="3"
							></el-option>
						</el-select>
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('agent.audit_status') + ':'">
					<el-select
						v-model="queryData.auditStatus"
						clearable
						collapse-tags
						:placeholder="$t('common.all')"
						style="width: 120px"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in auditStatus"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item :label="$t('agent.lock_status') + ':'">
					<el-select
						v-model="queryData.lockStatus"
						:popper-append-to-body="false"
						:placeholder="$t('common.all')"
						clearable
						style="width: 120px"
					>
						<el-option
							v-for="item in lockOrderType"
							:key="item.code"
							:label="item.description"
							:value="Number(item.code)"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item style="margin-left: 8px">
					<el-button
						type="primary"
						icon="el-icon-search"
						:disabled="loading"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
				</el-form-item>
			</el-form>
		</template>
		<template slot="content">
			<el-table
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				size="mini"
				class="small-size-table"
				:data="dataList"
				style="width: 100%"
				:max-height="tableHeight"
				:header-cell-style="getRowClass"
				highlight-current-row
			>
				<el-table-column
					align="center"
					:label="$t('common.lock_order')"
					width="60"
				>
					<template slot-scope="scope">
						<el-checkbox
							v-if="
								hasPermission('202021') &&
									(scope.row.auditStatus === 0 ||
										scope.row.auditStatus === 1)
							"
							v-model="scope.row.lockStatus"
							:disabled="disabledStatus(scope.row)"
							@change="lockChange(scope.row)"
						></el-checkbox>
					</template>
				</el-table-column>
				<el-table-column
					prop="auditStep"
					align="center"
					:label="$t('common.actions')"
					:width="getWidth(100, 1.8)"
				>
					<template slot-scope="scope">
						<!-- 待处理与处理中 -->
						<template
							v-if="
								(scope.row.auditStatus === 0 ||
									scope.row.auditStatus === 1) &&
									hasPermission('202022')
							"
						>
							<el-button
								:class="auditBtnStatus(scope.row)"
								type="primary"
								size="medium"
								@click="goDetail(scope.row)"
							>
								{{ $t('agent.audit') }}
							</el-button>
						</template>
						<template
							v-if="
								(scope.row.auditStatus === 2 ||
									scope.row.auditStatus === 3) &&
									hasPermission('202023')
							"
						>
							<el-button
								type="success"
								size="medium"
								@click="goDetail(scope.row)"
							>
								{{ $t('agent.statement_view') }}
							</el-button>
						</template>
					</template>
				</el-table-column>
				<el-table-column
					prop="auditNum"
					align="center"
					:label="$t('agent.audit_number')"
					min-width="220"
				>
					<template slot-scope="scope">
						<span>{{ scope.row.eventId || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="applyType"
					align="center"
					:label="$t('agent.audit_apply_time')"
					min-width="150px"
				>
					<template slot-scope="scope">
						{{
							typeFilter(scope.row.applyType, 'updProxyApplyType')
						}}
					</template>
				</el-table-column>
				<el-table-column align="center" width="130">
					<template slot="header">
						<span>
							{{ $t('agent.agent_account') }}
						</span>
					</template>
					<template slot-scope="scope">
						<Copy
							v-if="!!scope.row.userName"
							:title="scope.row.userName"
							:copy="copy"
						/>
						<span v-else>-</span>
					</template>
				</el-table-column>
				<el-table-column align="center" min-width="130">
					<template slot="header">
						<span>
							{{ $t('agent.agent_level') }}
							<br />
							{{ $t('agent.level_name') }}
						</span>
					</template>
					<template slot-scope="scope">
						{{ scope.row.proxyLevel }}/
						{{ scope.row.proxyLevelName }}
					</template>
				</el-table-column>
				<el-table-column
					prop="merchantId"
					align="center"
					:label="$t('common.belong_merchant')"
					width="230"
				>
					<template slot-scope="scope">
						<span>
							{{ merchantFilter(scope.row.merchantId) }}
						</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="applyName"
					align="center"
					:label="$t('agent.apply_people')"
					min-width="130px"
				>
					<template slot-scope="scope">
						<span>{{ scope.row.applyName || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="applyTime"
					align="center"
					min-width="180"
					:label="$t('agent.apply_time')"
				>
					<template slot-scope="scope">
						<span>{{ scope.row.createdAt || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="applyInfo"
					align="center"
					:label="$t('agent.apply_info')"
					min-width="180px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>{{ scope.row.applyRemark || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					align="center"
					:label="$t('agent.audit_status')"
					width="120"
				>
					<template slot-scope="scope">
						<span
							v-if="Number(auditStatus) !== 0"
							:class="matchAuditStatus(scope.row.auditStatus)"
						>
							{{
								typeFilter(
									scope.row.auditStatus,
									'updMemberAuditStatus'
								)
							}}
						</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="lockDatetime"
					align="center"
					min-width="180"
					:label="$t('agent.lock_order_time')"
				>
					<template slot-scope="scope">
						<span>{{ scope.row.lockDatetime || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" min-width="170px">
					<template slot="header">
						<span>
							{{ $t('agent.audit_people') }}
							<br />
							{{ $t('agent.audit_complete_time') }}
						</span>
					</template>
					<template slot-scope="scope">
						{{ scope.row.auditName || scope.row.lockName || '-' }}
						<p>{{ scope.row.auditDatetime || '-' }}</p>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				:current-page.sync="pageNum"
				class="pageValue"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size="pageSize"
				:page-sizes="pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination>
		</template>
		<agencyEditDetail
			slot="nonListContent"
			:type="type"
			:rowData="rowData"
			@goBack="goBack"
		></agencyEditDetail>
	</main-content>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { getUsername, getUserInfo } from '@/utils/auth'
import { routerNames } from '@/utils/consts'
import MainContent from '@/components/MainContent/index.vue'

const end = () =>
	dayjs()
		.endOf('day')
		.valueOf()
const start = () =>
	dayjs()
		.startOf('day')
		.valueOf()
export default {
	name: routerNames.agencyEdit,
	components: {
		MainContent,
		agencyEditDetail: () => import('./components/agencyEditDetail')
	},
	mixins: [list],
	data() {
		this.loadData = this.throttle(this.loadData, 1000)
		this.lockChange = this.throttle(this.lockChange, 1000)
		return {
			tableHeight: 480,
			otherDependenComponents: [],
			queryData: {
				timeType: 0,
				operatorType: 0,
				operatorName: undefined,
				userName: undefined,
				applyType: undefined,
				auditStatus: undefined,
				applyName: undefined,
				auditName: undefined,
				auditNum: undefined
			},
			type: true,
			showDetail: false,
			searchTime: [start(), end()],
			rowData: {},
			now: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
			name: '',
			userInfo: {},
			dataList: []
		}
	},
	computed: {
		accountType() {
			return this.globalDics.accountType || []
		},
		porxyApplyTypeArr() {
			const arr = this.globalDics.porxyApplyType || []
			if (arr.length > 0) {
				arr.sort((a, b) => {
					return a.code - b.code
				})
			}
			return arr
		},
		auditStatus() {
			return this.globalDics.updMemberAuditStatus || []
		},
		auditStepType() {
			return this.globalDics.auditStepType || []
		},
		lockOrderType() {
			return this.globalDics.lockStatus || []
		},
		applyType() {
			return this.globalDics.applyType || []
		},
		accountTypeArr() {
			return this.globalDics.accountType || []
		}
	},
	mounted() {
		this.name = getUsername()
		this.userInfo = getUserInfo()
	},
	methods: {
		disabledStatus(row) {
			return (
				row.lockStatus &&
				(row.lockAccountId
					? +row.lockAccountId !== +this.userInfo.id
					: row.lockName !== this.name)
			)
		},
		auditBtnStatus(row) {
			return row.lockStatus === 0 ||
				(row.lockAccountId
					? +row.lockAccountId !== +this.userInfo.id
					: row.lockName !== this.name)
				? 'dis'
				: ''
		},
		trimLR() {
			this.queryData.operatorName = this.queryData.operatorName.replace(
				/\s+/g,
				''
			)
		},
		matchAuditStatus(auditStatus) {
			// 审核状态（0-待处理 1-处理中，2-审核通过，3-审核拒绝）
			switch (auditStatus) {
				case 0:
					return 'deleteRgba'
				case 1:
					return 'lockingRgba'
				case 2:
					return 'disableRgba'
				case 3:
					return 'normalRgba'
				default:
					return 'normalRgba'
			}
		},
		loadData() {
			this.loading = true
			const [startTime, endTime] = this.searchTime || []
			let params = {
				...this.queryData,
				timeStart: startTime
					? dayjs(startTime).format('YYYY-MM-DD HH:mm:ss')
					: undefined,
				timeEnd: endTime
					? dayjs(endTime).format('YYYY-MM-DD HH:mm:ss')
					: undefined
			}
			params = {
				...this.getParams(params)
			}
			this.$api
				.proxyDataPage(params)
				.then((res) => {
					if (res?.code === 200) {
						this.now = dayjs(new Date()).format(
							'YYYY-MM-DD HH:mm:ss'
						)
						const { record, totalRecord } = res.data || {}
						this.loading = false
						this.dataList = Array.isArray(record) ? record : []
						if (this.dataList.length) {
							this.dataList.forEach((item) => {
								if (Number(item.lockStatus) === 1) {
									item.lockStatus = true
								} else {
									item.lockStatus = false
								}
							})
						}
						this.total = totalRecord || 0
					} else {
						this.loading = false
						this.$message({
							message:
								res?.msg ||
								this.$t('agent.interface_exceptions'),
							type: 'error'
						})
					}
				})
				.catch(() => {
					this.loading = false
				})
		},
		changeTableSort2({ column, prop, order }) {
			this.pageNum = 1
			this.queryData.orderKey = prop === 'applyTime' ? 1 : 2
			if (order === 'ascending') {
				// 升序
				this.queryData.orderType = 'asc'
			} else if (column.order === 'descending') {
				// 降序
				this.queryData.orderType = 'desc'
			} else {
				delete this.queryData.orderKey
				delete this.queryData.orderType
			}
			this.loadData()
		},
		goDetail(row) {
			// this.type = Number(row.auditStep) === 1 && row.auditName === this.name
			this.type =
				Number(row.auditStatus) === 0 || Number(row.auditStatus) === 1
			this.rowData = row
			this.showDetail = true
		},
		goBack() {
			this.dataList = []
			this.showDetail = false
			this.loadData()
		},
		reset() {
			this.queryData = {
				timeType: 0,
				operatorType: 0,
				userName: undefined,
				applyType: undefined,
				applyName: undefined,
				auditName: undefined,
				lockStatus: '',
				auditNum: undefined
			}
			this.searchTime = [start(), end()]
			this.loadData()
		},
		lockChange(val) {
			const loading = this.$loading({
				lock: true,
				text: 'Loading',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			})
			this.$api
				.proxyDataLock({
					id: val.id,
					merchantId: val.merchantId,
					lockStatus: val.lockStatus ? 1 : 0
				})
				.then((res) => {
					if (res?.code === 200) {
						loading.close()
						this.$message({
							type: 'success',
							message: this.$t('common.action_success')
						})
						this.loadData()
					} else {
						loading.close()
						this.$message({
							message:
								res?.msg ||
								this.$t('agent.interface_exceptions'),
							type: 'error'
						})
						this.loadData()
					}
				})
				.catch(() => {
					this.loadData()
					loading.close()
				})
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	text-align: center;
	color: #909399;
	font-weight: 700;
}
::v-deep .caret-wrapper {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
.data-refresh {
	margin-top: 0;
	padding-bottom: 20px;
}
::v-deep .el-input-group__prepend {
	background-color: #fff;
}
</style>
